package com.wzt.lianfirecontrol.adapter.xuncha.devicexuncha;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.wzt.lianfirecontrol.R;
import com.wzt.lianfirecontrol.activity.BaseActivity;
import com.wzt.lianfirecontrol.adapter.BaseRecyclerAdapter;
import com.wzt.lianfirecontrol.bean.ConstData;
import com.wzt.lianfirecontrol.bean.recode.function.xuncha.devicexuncha.XunChaDeviceListModel;
import com.wzt.lianfirecontrol.utils.StringUtils;
import com.wzt.lianfirecontrol.utils.Utils;

import java.util.HashMap;

public class DeviceListAdapter extends BaseRecyclerAdapter<XunChaDeviceListModel> {

    private Context context;

    private boolean needSetTopMargin = false;

    public boolean isNeedSetTopMargin() {
        return needSetTopMargin;
    }

    public void setNeedSetTopMargin(boolean needSetTopMargin) {
        this.needSetTopMargin = needSetTopMargin;
    }

    public DeviceListAdapter(Context context) {
        this.context = context;
    }

    @Override
    public RecyclerView.ViewHolder onCreate(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_info_list, parent, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBind(RecyclerView.ViewHolder viewHolder, int RealPosition, XunChaDeviceListModel data) {
        if (viewHolder instanceof MyViewHolder){
            setItemData((MyViewHolder)viewHolder, data, RealPosition);
        }
    }

    private class MyViewHolder extends RecyclerView.ViewHolder{
        private LinearLayout ll_item_content;
        private TextView tv_title;
        private TextView tv_status;
        private TextView tv_flag;
        private ImageView iv_show_goto;
        private LinearLayout ll_location;
        private ImageView iv_location;
        private TextView tv_location;
        private TextView tv_nfc_flag;
        private ImageView iv_videos;

        public MyViewHolder(View itemView) {
            super(itemView);
            initItemView(this, itemView);
        }
    }

    private void initItemView(MyViewHolder myViewHolder, View itemView) {
        myViewHolder.ll_item_content = itemView.findViewById(R.id.ll_item_content);
        myViewHolder.tv_title = itemView.findViewById(R.id.tv_title);
        myViewHolder.tv_status = itemView.findViewById(R.id.tv_status);
        myViewHolder.tv_flag = itemView.findViewById(R.id.tv_flag);
        myViewHolder.iv_show_goto = itemView.findViewById(R.id.iv_show_goto);
        myViewHolder.ll_location = itemView.findViewById(R.id.ll_location);
        myViewHolder.iv_location = itemView.findViewById(R.id.iv_location);
        myViewHolder.tv_location = itemView.findViewById(R.id.tv_location);
        myViewHolder.tv_nfc_flag = itemView.findViewById(R.id.tv_nfc_flag);
        myViewHolder.iv_videos = itemView.findViewById(R.id.iv_videos);

    }

    private void setItemData(MyViewHolder viewHolder, XunChaDeviceListModel data, int position) {
        viewHolder.iv_show_goto.setVisibility(View.VISIBLE);
        RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) viewHolder.ll_item_content.getLayoutParams();
        if (0 == position && needSetTopMargin){
            layoutParams.setMargins(Utils.dip2px(context, 0), Utils.dip2px(context, 15),
                    Utils.dip2px(context, 0), Utils.dip2px(context, 15));
        }else {
            layoutParams.setMargins(Utils.dip2px(context, 0), Utils.dip2px(context, 0),
                    Utils.dip2px(context, 0), Utils.dip2px(context, 15f));
        }
        if (StringUtils.isEmpty(data.getEquName())){
            viewHolder.tv_title.setVisibility(View.GONE);
        }else {
            viewHolder.tv_title.setVisibility(View.VISIBLE);
            viewHolder.tv_title.setText(data.getEquName());
        }
        if (StringUtils.isEmpty(data.getStatus())){
            viewHolder.tv_status.setVisibility(View.GONE);
        }else {
            viewHolder.tv_status.setVisibility(View.VISIBLE);
            if ("1".equals(data.getStatus())){
                viewHolder.tv_status.setText("待检查");
                viewHolder.tv_status.setTextColor(Color.parseColor("#FD9D3F"));
                viewHolder.iv_show_goto.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        HashMap<String, String> url_params = new HashMap<>();
                        Bundle bundle = new Bundle();
                        bundle.putSerializable("deviceListModel", data);
                        bundle.putString("recordId", data.getId());
                        ((BaseActivity)context).setClickAction(ConstData.SHOWFRAG, ConstData.ADDXUNCHADETAILQES, "问题详情", url_params, bundle);
                    }
                });
            }else {
                viewHolder.tv_status.setText("已完成");
                viewHolder.tv_status.setTextColor(Color.parseColor("#5573FF"));
                viewHolder.iv_show_goto.setVisibility(View.INVISIBLE);
            }
        }

        // 有nfc的功能不显示点击按钮
        if (StringUtils.isEmpty(data.getFrid())) {
            viewHolder.tv_nfc_flag.setVisibility(View.GONE);
            if (data.getStatus().equals("2")){
                viewHolder.iv_show_goto.setVisibility(View.GONE);
            }else {
                viewHolder.iv_show_goto.setVisibility(View.VISIBLE);
                viewHolder.iv_show_goto.setImageResource(R.mipmap.icon_add);
            }
        } else {
            viewHolder.tv_nfc_flag.setVisibility(View.VISIBLE);
            viewHolder.iv_show_goto.setVisibility(View.GONE);
        }
        viewHolder.iv_videos.setVisibility(View.GONE);

        if (StringUtils.isEmpty(data.getAddress())){
            viewHolder.ll_location.setVisibility(View.GONE);
        }else {
            viewHolder.iv_location.setVisibility(View.VISIBLE);
            viewHolder.ll_location.setVisibility(View.VISIBLE);
            StringBuilder stringBuilder = new StringBuilder();
            if (!StringUtils.isEmpty(data.getBuildingName())){
                stringBuilder.append(data.getBuildingName());
            }
            if (!StringUtils.isEmpty(data.getFloorName())){
                stringBuilder.append(data.getFloorName());
            }
            if (!StringUtils.isEmpty(data.getAddress())){
                stringBuilder.append(data.getAddress());
            }
            viewHolder.tv_location.setText(stringBuilder.toString());
        }
    }
}
